<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class reputation_class extends AWS_MODEL
{
	public function get_reputation_topic($uid)
	{
		return $this->fetch_all('reputation_topic', ' uid = ' . intval($uid), 'topic_count DESC');
	}

	public function cron($start = 0, $limit = 100)
	{
		//取出用户
		if ($start == 0)
		{
			$start = $this->model('db_cache')->load('reputation_cron_user_start');
			
			$start = intval($start);
		}
		
		$users_list = $this->model('account')->get_user_list(null, $start . ',' . $limit);
		
		$reputation_function = get_setting('reputation_function');
		
		if (! $users_list)
		{
			$users_list = array();
		}
		
		foreach ($users_list as $key => $val)
		{
			$user_topics = null;
			$user_reputation = 0;
			$user_reputation_category = null;
			
			//取出用户回复
			$users_anwser = $this->model('answer')->get_answers(false, 'uid = ' . $val['uid']);
			
			if ($users_anwser)
			{
				unset($answer_ids);
				unset($question_ids);
				
				foreach ($users_anwser as $an_key => $an_val)
				{
					$answer_ids[] = $an_val['answer_id'];
					$question_ids[] = $an_val['question_id'];
				}
				
				if ($question_ids)
				{
					$questions = $this->model('question')->get_question_info_by_ids($question_ids);
				}
				
				if ($answer_ids)
				{
					$vote_agree_users = $this->model('answer')->get_vote_agree_by_answer_ids($answer_ids);
					$vote_against_users = $this->model('answer')->get_vote_against_user_by_answer_ids($answer_id);
				}
				
				foreach ($users_anwser as $an_key => $an_val)
				{
					//$reputation_function=get_setting('reputation_function');
					$best_answer_reput = get_setting('best_answer_reput');
					
					$publisher_reputation_factor = get_setting('publisher_reputation_factor');
					//$publisher_agaist_reput = get_setting('publisher_agaist_reput');
					

					//[最佳回复]+[赞同]-[反对]+[发起者赞同]-[发起者反对];
					
					$answer_id = $an_val['answer_id'];
					$question_id = $an_val['question_id'];
					
					$s_best_answer = 0;
					$s_agree_count = 0;
					$s_against_count = 0;
					$s_initiate_agree_count = 0;
					$s_initiate_against_count = 0;
					$tmp_reputation = 0;
					$best_answer_count = 0;
					
					if (!$questions[$question_id])
					{
						continue;
					}
					
					//获取问题的话题
					if ($questions[$question_id]['best_answer'] == $answer_id)
					{
						$s_best_answer = 1;
						$best_answer_count = 1;
					}
					
					//$s_agree_count	=$an_val['agree_count'];
					//$s_against_count = $an_val['against_count'];
					

					$s_agree_count = 0;
					$s_against_count = 0;
					
					if ($vote_agree_users[$answer_id])
					{
						foreach ($vote_agree_users[$answer_id] as $vote_key => $vote_val)
						{
							if ($questions[$question_id]['published_uid'] == $vote_val['uid'])
							{
								continue;
							}
							
							$s_agree_count += $s_agree_count + $vote_val['reputation_factor'];
						}

						if (array_key_exists($questions[$question_id]['published_uid'], $vote_agree_users[$answer_id]))
						{
							$s_initiate_agree_count = 1;
						}
					}
					
					if ($vote_against_users[$answer_id])
					{
						foreach ($vote_against_users[$answer_id] as $vote_key => $vote_val)
						{
							if ($questions[$question_id]['published_uid'] == $vote_val['uid'])
							{
								continue;
							}
							
							$s_against_count += $s_against_count + $vote_val['reputation_factor'];
						
						}
					
						if (array_key_exists($questions[$question_id]['published_uid'], $vote_against_users[$answer_id]))
						{
							$s_initiate_against_count = 1;
						}
					}
					
					if ($s_initiate_agree_count > 0)
					{
						$s_agree_count = $s_agree_count - 1;
					}
					
					if ($s_initiate_against_count > 0)
					{
						$s_against_count = $s_against_count - 1;
					}
					
					//[最佳回复]*3+[赞同]*1-[反对]*1+[发起者赞同]*2-[发起者反对]*1
					//$reputation_function=str_replace('[最佳回复]', $s_best_answer, $reputation_function);
					//$reputation_function=str_replace('[赞同]', $s_agree_count, $reputation_function);
					//$reputation_function=str_replace('[反对]', $s_against_count, $reputation_function);
					//$reputation_function=str_replace('[发起者赞同]', $s_initiate_agree_count, $reputation_function);
					//$reputation_function=str_replace('[发起者反对]', $s_initiate_against_count, $reputation_function);
					

					$tmp_reputation = ($s_best_answer * $best_answer_reput) + $s_agree_count - $s_against_count + ($s_initiate_agree_count * $publisher_reputation_factor) - ($s_initiate_against_count * $publisher_reputation_factor);
					
					//eval('$tmp_reputation='.$reputation_function.';');
					

					if ($tmp_reputation <= 0)
					{
						$tmp_reputation = 0;
					}
					else
					{
						$tmp_reputation = $tmp_reputation + 0.5;
						$tmp_reputation = log($tmp_reputation, 3);
					}
					
					if ($tmp_reputation > 0)
					{
						$topics = $this->model('question_topic')->get_question_topic_by_question_id($question_id);
						
						if ($topics)
						{
							foreach ($topics as $topic)
							{
								if (! $topic['topic_id'])
								{
									continue;
								}
								
								$count = intval($user_topics[$topic['topic_id']]['count']) + 1;
								$agree_count = intval($user_topics[$topic['topic_id']]['agree_count']) + $an_val['agree_count'];
								$thanks_count = intval($user_topics[$topic['topic_id']]['thanks_count']) + $an_val['thanks_count'];
								$reputation = intval($user_topics[$topic['topic_id']]['reputation']) + intval($tmp_reputation);
								$best_answer_count_all = intval($user_topics[$topic['topic_id']]['best_answer_count']) + intval($best_answer_count);
								$user_topics[$topic['topic_id']] = array(
									'topic_id' => $topic['topic_id'], 
									'count' => $count, 
									'agree_count' => $agree_count, 
									'thanks_count' => $thanks_count, 
									'reputation' => $reputation, 
									'best_answer_count' => $best_answer_count_all
								);
							
							}
						}
					}
					
					$user_reputation = $user_reputation + $tmp_reputation; //$s_best_answer+$s_agree_count-$s_against_count+$s_initiate_agree_count-$s_initiate_against_count;
					

					if ($questions[$question_id]['category_id'] > 0)
					{
						$user_reputation_category[$questions[$question_id]['category_id']]['reputation'] = $user_reputation_category[$questions[$question_id]['category_id']]['reputation'] + $tmp_reputation;
						$user_reputation_category[$questions[$question_id]['category_id']]['agree_count'] = $user_reputation_category[$questions[$question_id]['category_id']]['agree_count'] + $an_val['agree_count'];
						$user_reputation_category[$questions[$question_id]['category_id']]['questions'][$an_val['question_id']] = $an_val['question_id'];
					}
				
				}
			}
			
			if (is_array($user_topics))
			{
				
				$user_topics = aasort($user_topics, array(
					'-count'
				));
				
				if ($user_topics)
				{
					$user_topics = array_slice($user_topics, 0, 20);
				}
				else
				{
					$user_topics = array();
				}
				
				$this->delete('reputation_topic', 'uid = ' . $val['uid']);
				
				foreach ($user_topics as $t_key => $t_val)
				{
					
					$insert_array = null;
					$insert_array['uid'] = $val['uid'];
					$insert_array['topic_id'] = $t_val['topic_id'];
					$insert_array['topic_count'] = $t_val['count'];
					$insert_array['update_time'] = time();
					$insert_array['agree_count'] = $t_val['agree_count'];
					$insert_array['thanks_count'] = $t_val['thanks_count'];
					$insert_array['best_answer_count'] = $t_val['best_answer_count'];
					$insert_array['reputation'] = intval(round($t_val['reputation']));
					$this->insert('reputation_topic', $insert_array);
				}
			}
			
			if (is_array($user_reputation_category))
			{
				
				$this->delete('reputation_category', 'uid = ' . $val['uid']);
				
				foreach ($user_reputation_category as $t_key => $t_val)
				{
					
					$insert_array = null;
					$insert_array['uid'] = $val['uid'];
					$insert_array['update_time'] = time();
					$insert_array['category_id'] = $t_key;
					$insert_array['reputation'] = intval(round($t_val['reputation']));
					$insert_array['agree_count'] = $t_val['agree_count'];
					$insert_array['question_count'] = count($t_val['questions']);
					
					$this->insert('reputation_category', $insert_array);
				}
			
			}
			
			$this->model('account')->update_users_fields(array(
				'reputation' => intval(round($user_reputation)), 
				'reputation_update_time' => time()
			), $val['uid']);
			
			$this->model('account')->update_user_reputation_group($val['uid']);
		
		}
		
		//重置数值
		if (count($users_list) < $limit)
		{
			$start = 0;
		}
		else
		{
			$start = $start + $limit;
		}
		
		//p('___' . $start);
		$this->model('db_cache')->save('reputation_cron_user_start', $start, 60 * 60 * 24);
	}
}